<template>
  <Popup :changeShowDialog="props.changeShowDialog">
    <template #header>
      <span class="dialog-title">
        {{ userAddress ? '修改' : '添加' }}收货地址
      </span>
    </template>
    <template #main>
      <div class="dialog-body">
        <div class="address-add">
          <div class="item">
            <Input
              class="col mr"
              v-model:value="data!.address_name">
              姓名
            </Input>
            <Input
              class="col"
              v-model:value="data!.address_phone">
              手机号
            </Input>
          </div>
          <div class="item address-info-box">
            <Input
              v-model:value="addressString"
              @click.native="changeIsShow(true)"
              class="col"
              :placeholder="'选择省/市/区/街道'"></Input>
            <div
              class="address-select-box"
              v-if="isShow">
              <i
                class="iconfont"
                @click="changeIsShow(false)">
                ×
              </i>
              <AddressSelect :changeIsShow="changeIsShow"></AddressSelect>
            </div>
          </div>
          <div class="item">
            <Input
              class="col"
              :type="'textarea'"
              v-model:value="data!.address_detail">
              详细地址
            </Input>
          </div>
        </div>
      </div>
    </template>
    <template #footer>
      <Button
        @click.native="addAddress"
        :type="'primary'"
        :style="{
          height: 40 + 'px',
          width: 160 + 'px',
          'margin-right': 20 + 'px'
        }">
        确定
      </Button>
      <Button
        @click.native="changeShowDialog"
        :type="'gray'"
        :style="{ height: 40 + 'px', width: 160 + 'px' }">
        取消
      </Button>
    </template>
  </Popup>
</template>

<script setup lang="ts">
import { ref, computed, watchEffect } from 'vue'
import useStore from '@/store'
import Popup from '@/components/popup/index.vue'
import Button from '@/components/button/index.vue'
import Input from '@/components/input/index.vue'
import AddressSelect from '@/components/address/addressSelect/index.vue'
import { userAddress } from '@/model/addressModel'
const { addressStore, userStore } = useStore()
const props = defineProps({
  changeShowDialog: Function
})
const { userAddress } = addressStore
const data = ref({ ...userAddress })
const addressString = computed(
  () =>
    data.value!.province_name &&
    `${data.value!.province_name} ${data.value!.city_name} ${
      data.value!.county_name
    } ${data.value!.way_name}`
)
watchEffect(() => {
  if (addressStore.userAddress) {
    data.value.province_name = addressStore.userAddress.province_name
    data.value.city_name = addressStore.userAddress.city_name
    data.value.county_name = addressStore.userAddress.county_name
    data.value.way_name = addressStore.userAddress.way_name
  }
})
const isShow = ref<boolean>(false)
const changeIsShow = (flag: boolean) => {
  isShow.value = flag
}
const changeShowDialog = () => {
  props.changeShowDialog && props.changeShowDialog()
  addressStore.userAddress = undefined
}
const addAddress = () => {
  data.value && data.value.address_id
    ? addressStore.updAddress(data.value as userAddress)
    : addressStore.insAddress(
        data.value as userAddress,
        userStore.userInfo!.user_id
      )
  changeShowDialog()
}
</script>

<style lang="less" scoped>
.dialog-title {
  font-size: 18px;
  font-weight: 400;
  color: #424242;
  vertical-align: middle;
}
.dialog-body {
  padding: 20px 0;
  .address-add {
    .item {
      display: flex;
      margin-bottom: 14px;
      .col {
        flex: 1;
      }
      .mr {
        margin-right: 14px;
      }
    }
    .address-info-box {
      position: relative;
      .address-select-box {
        .iconfont {
          position: absolute;
          cursor: pointer;
          top: 10px;
          right: 5px;
          vertical-align: middle;
          font-size: 18px;
          width: 18px;
          height: 18px;
          line-height: 18px;
          font-weight: 200;
          transition: 0.5s;
          color: #b0b0b0;
          &:hover {
            color: #000;
          }
        }
        position: absolute;
        top: 0;
        left: 0;
        width: 100%;
        background: #fff;
        padding-top: 30px;
        border: 1px solid #ff6700;
        z-index: 3;
      }
    }
  }
}
</style>
